define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true, operate: false},
                        {field: 'group.name', title: __('Group'), operate: false},
                        {field: 'company_name', title: __('直属机构'), operate: false},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'parent.type', title: __('上级性质'),formatter:function (value) {
                                return value == 1 ? "<p style='color: #00c1fc;'>机构</p>" : "<p style='color: #ea3a4e;'>代理</p>" ;
                            },operate: false},
                        {field: 'parent.name', title: __('上级名称'), operate: false},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'is_setting', title: __('设置政策'),formatter:function (value) {
                                return value == 1 ? "<p style='color: lawngreen;'>已设置</p>" : "<p style='color: red;'>未设置</p>" ;
                            },operate: false},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')},operate: false},
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'bank',
                                title: __('收款账号'),
                                // text: __('收款信息'),
                                icon: 'fa fa-bitcoin',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'user/user/bank'
                            },{
                                name: 'qrcode',
                                title: __('推广码'),
                                // text: __('接收'),
                                icon: 'fa fa-qrcode',
                                classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
                                url: 'user/user/qr',
                                extend: 'data-area=\'["450px", "450px"]\''
                            },{
                                name: 'cut',
                                title: __('新增'),
                                // text: __('接收'),
                                icon: 'fa fa-cut',
                                classname: 'btn btn-primary btn-xs btn-detail btn-dialog',
                                url: 'user/user/cut',
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        cut: function () {
            Controller.api.bindevent();
        },
        policy: function () {
            $(document).on('click', "#default", function () {
                let id = $(this).data("id");
                let product_id = $(this).data("product");
                layer.confirm('确定操作吗?', {icon: 3, title:'提示'}, function(index){
                    Fast.api.ajax({
                        url: "user/user/policy?ids=" + Config.user,
                        async: false,
                        data: {relation_id: id,product_id: product_id},
                    }, function (data, ret) {
                        // setTimeout(function () {
                        //     var index = parent.Layer.getFrameIndex(window.name);
                        //     parent.Layer.close(index);
                        // }, 2000)
                    });
                    layer.close(index);
                    return false;
                });
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});